<?php

/*
 * Luck 28	API 接口操作模型
 * Author	1041865683@qq.com
 * Date		2017.5.3
 */


/*
 * 手机验证码 发送
 * 网易云信
 * @param  $mobile		手机号
 * @param  $templateid	短信模板ID
 * @return $json		发送返回信息	Json
 */
function mobile_vcode_send($mobile,$templateid=3064305){
	$data  = array('mobile'=> $mobile,'templateid'=>$templateid);
	$data = http_build_query($data);
	$opts = mobile_vcode_header($data);
	$context = stream_context_create($opts);
	$html = file_get_contents("http://api.netease.im/sms/sendcode.action", false, $context);
	$json = json_decode($html);
	return $json;
}


/*
 * 手机验证码 检测
 * 网易云信
 * @param  $mobile		 手机号
 * @param  $vcode		 验证码			
 * @return $bool		 发送返回信息	Boolean
 */
function mobile_vcode_check($mobile,$vcode){
	$data  = array('mobile'=> $mobile,'code'=>$vcode);
	$data = http_build_query($data);
	$opts = mobile_vcode_header($data);
	$context = stream_context_create($opts);
	$html = file_get_contents("http://api.netease.im/sms/verifycode.action", false, $context);
	$json = json_decode($html);
	$bool = $json->code==200 ? true : false;
	return $bool;
}


/*
 * 手机通知 发送
 * 网易云信
 * @param  $mobile		手机号
 * @param  $templateid	短信模板ID
 * @return $json		发送返回信息	Json
 */
function mobile_notice_send($mobile,$templateid,$param){
	$data  = array('mobiles'=> $mobile,'templateid'=>$templateid,'params'=>$param);
	$data = http_build_query($data);
	$opts = mobile_vcode_header($data);
	$context = stream_context_create($opts);
	$html = file_get_contents("http://api.netease.im/sms/sendtemplate.action", false, $context);
	$json = json_decode($html);
	return $json;
}


/*
 * 手机验证码 Header头信息
 * 网易云信
 * @param  $data		要发送的头信息
 * @return $opts		发送返回信息	Array
 */
function mobile_vcode_header($data){
	global $conf,$time;
	$rand = md5(rand(11,99).rand(111,999).rand(172,826));
	$sign = strtolower(sha1($conf['mobile_secret'].$rand.$time));
	$opts = array (
		'http' => array(
			'method' => 'POST',
			'header' => array(
				'Content-Type:application/x-www-form-urlencoded;charset=utf-8',
				"AppKey:".$conf['mobile_appkey'],
				"Nonce:".$rand,
				"CurTime:".$time,
				"CheckSum:".$sign
			),
			'content' =>  $data
		),
	);
	return $opts;
}


/*
 * 发送邮件验证码
 * 邮箱
 * @param  $email	目标邮箱
 * @return $r		发送返回信息	String
 */
function email_vcode_send($email){
	global $conf,$time;
	$tokenkey = md5($conf['auth_key']);
	$smtp = include _include(APP_PATH.'conf/smtp.conf.php');
	$r = user_read_by_email($email);
	$r AND message('email', lang('email_is_in_use'));
	$token = rand(1000, 9999).$email;
	$code = xn_encrypt($token, $tokenkey);
	setcookie('email_bind', $code, $time + 600, $conf['cookie_path']);
	$subject = lang('email_bind_template', array('rand'=>$code, 'sitename'=>$conf['sitename']));
	$message = $subject;
	$r = xn_send_mail($smtp, $conf['sitename'], $email, $subject, $message);
	return $r;
}


/*
 * 获取登陆地址
 * 微信
 * @return		返回微信登陆URL		String
 */
function wx_login_url(){
	global $conf, $recommend;
	$encode = urlencode("http://".$conf['domin']."/api-wx_login.htm?recommend=".$recommend);
	$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$conf['wx_pay_appid']."&redirect_uri=".$encode."&response_type=code&scope=snsapi_userinfo&state=lucky#wechat_redirect";
	return $url;
}


?>